#include "Interface.h"
#include <ncurses.h>
#include "locale.h"
#include <thread>
#include "unistd.h"
struct InterfacePrivate
{
    //界面指针
    WINDOW *win;
    int startx, starty;
};

Interface::Interface():p(new InterfacePrivate)
{
    setlocale(LC_ALL, ""); //设置支持本地系统语言
    initscr();
}

Interface::~Interface()
{
    delete p;
    endwin();
}

void Interface::clear()
{
    refresh();
    //指针释放
    delwin(p->win);
    p->win = nullptr;
}

void Interface::drawBox(int width, int height)
{
    p->starty = (LINES - height) / 2;  // 计算居中位置
    p->startx = (COLS - width) / 2;
    p->win = newwin(height, width, p->starty, p->startx);
    
    box(p->win, 0, 0);
    wrefresh(p->win);
}

void Interface::drawText(int x, int y, const std::string &text)
{
    mvwprintw(p->win, y, x, "%s", text.c_str());
    wrefresh(p->win);
}


void Interface::EnableKeyboard(bool enable)
{
    keypad(p->win, enable);
}

void Interface::ShowCursor(bool show)
{
    curs_set(show);
}

std::string Interface::GetInput(int x, int y)
{
    char buffer[256];
    mvwgetnstr(p->win, y, x, buffer, 255);
    wrefresh(p->win);

    return std::string(buffer);
}

std::string Interface::GetPassword(int x, int y)
{
    int index = 0;
    char ch;
    char password[256];
    wmove(p->win, y, x);
    while ((ch = wgetch(p->win)) != '\n' && index < 255) {
        if (ch == KEY_BACKSPACE || ch == 127) 
        { 
            // 处理退格键
            if (index > 0) 
            {
                index--;
            }
            mvwaddch(p->win, y, x + index, ' '); // 清除当前位置
            mvwaddch(p->win, y, x + index+1, ' '); 
            mvwaddch(p->win, y, x + index+2, ' '); // 清除多余字符
            wmove(p->win, y, x + index);
            
        } else {
            password[index++] = ch;
            mvwaddch(p->win, y, x + index, ' '); // 清除当前位置
            // 显示星号而不是实际字符
            mvwaddch(p->win, y,x + index - 1, '*');
        }
        wrefresh(p->win);

    }
    password[index] = '\0'; // 添加字符串结束符
    return std::string(password);
}

void Interface::KeyboardEvent(const std::function<void(int)> &callback)
{
    
    int ch = '\n';
    while ( (ch = wgetch(p->win)) != '\n')
    {
        callback(ch);
    }
}

// 登陆界面
std::pair<std::string, std::string> CreateLoginInterface()
{
    Interface i;
    i.drawBox(40,10);
    i.drawText(15,1,"登陆界面");
    i.drawText(2,3,"用户名:");
    i.drawText(2,5,"密码:");
    i.drawText(5,7,"   登陆");
    i.drawText(25,7,"   退出");

    auto Name =  i.GetInput(10,3);
    auto passwd = i.GetPassword(10,5);

    i.drawText(5,7,"-> 登陆");
    i.drawText(25,7,"   退出");
    i.ShowCursor(false);
    i.EnableKeyboard(true);
    int choice = 0;
    i.KeyboardEvent([&i,&choice](int key)
    {
        switch (key) 
        {
            case Interface::Keyboard::Key_left:
            {
                choice = (choice - 1 + 2) % 2;
                break;
            }
            case Interface::Keyboard::Key_right:
                choice = (choice + 1) % 2;
                break;
        }
        i.drawText(5,7,(choice == 0) ? "-> 登陆" : "   登陆");
        i.drawText(25,7,(choice == 1) ? "-> 退出" : "   退出");
    });

    if(choice == 0)
    {
        i.drawText(15,9,"登陆中...");
    }
    else
    {
        i.drawText(15,9,"退出中");
    }
    sleep(2);
    i.clear();
    return {Name, passwd};
}


